package com.fjh.controller;

import com.fjh.config.ResourceServerConfig;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@RestController
@Api(tags = "文件上传")
@RequestMapping("admin/file")
public class FileController {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @Autowired
    private ResourceServerConfig resourceServerConfig;

    @PostMapping("upload/element")
    @ApiOperation("文件上传")
    public ResponseEntity<String> uploadFile(@RequestParam MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        String ext = StringUtils.getFilenameExtension(originalFilename);
        long size = file.getSize();
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String fullPath = fastFileStorageClient.uploadFile(inputStream, size, ext, null).getFullPath();
        return ResponseEntity.ok(resourceServerConfig.getServerPath()+fullPath);
    }



}
